﻿using Polaris.Domain.Model.Constant;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace Polaris.Domain.Model.Entity.Jwt
{
    /// <summary>
    /// 权限配置类型
    /// </summary>
    public static class ClaimType
    {
        /// <summary>
        /// 管理员权限配置
        /// </summary>
        private readonly static List<Claim> administrator = new List<Claim>() {
             new Claim(ClaimTypes.Name,RoleConstant.Administrator),
             new Claim(ClaimTypes.Role,RoleConstant.Administrator),
             new Claim(ClaimTypes.Role,RoleConstant.User),
             new Claim(ClaimTypes.Role,RoleConstant.Tourist),
        };

        /// <summary>
        /// 普通用户权限配置
        /// </summary>
        private readonly static List<Claim> user = new List<Claim>() {
             new Claim(ClaimTypes.Name,RoleConstant.User),
             new Claim(ClaimTypes.Role,RoleConstant.User),
             new Claim(ClaimTypes.Role,RoleConstant.Tourist),
        };

        /// <summary>
        /// 游客用户权限配置
        /// </summary>
        private readonly static List<Claim> tourist = new List<Claim>() {
             new Claim(ClaimTypes.Name,RoleConstant.Tourist),
             new Claim(ClaimTypes.Role,RoleConstant.Tourist),
        };

        /// <summary>
        /// 通过枚举获取对应的 Claim 集合副本
        /// </summary>
        /// <param name="roleType"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public static List<Claim> GetClaims(RoleType roleType)
        {
            return roleType switch
            {
                RoleType.Administrator => new List<Claim>(administrator),
                RoleType.User => new List<Claim>(user),
                RoleType.Tourist => new List<Claim>(tourist),
                _ => throw new ArgumentException("Invalid role type")
            };
        }

        /// <summary>
        /// 扩展方法：添加 NameIdentifier
        /// </summary>
        /// <param name="claims"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static List<Claim> WithUserId(this List<Claim> claims, string userId)
        {
            claims.Add(new Claim(ClaimTypes.NameIdentifier, userId));
            return claims;
        }

        /// <summary>
        /// 扩展方法：支持添加任意类型的 Claim
        /// </summary>
        /// <param name="claims"></param>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static List<Claim> WithClaim(this List<Claim> claims, string type, string value)
        {
            claims.Add(new Claim(type, value));
            return claims;
        }
    }

    public enum RoleType
    {
        Administrator,
        User,
        Tourist
    }
}
